package net.kazed.nextaction.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;

/**
 * List of TaskAttachment objects.
 */
public class TaskAttachmentList {

    private Task task;
    private List<TaskAttachment> attachments = new ArrayList<TaskAttachment>();
    private List<TaskAttachment> addedAttachments = new ArrayList<TaskAttachment>();
    private List<TaskAttachment> deletedAttachments = new ArrayList<TaskAttachment>();

    /**
     * Constructor.
     * @param contentResolver Retrieve attachments from this provider.
     * @param task Task that attachment belong to.
     */
    public TaskAttachmentList(ContentResolver contentResolver, Task task) {
        this.task = task;
        Uri itemUri = ContentUris.withAppendedId(TaskAttachment.URI_ATTACHMENTS_BY_TASK, task.getId());
        Cursor cursor = contentResolver.query(itemUri, TaskAttachment.FULL_PROJECTION.getFieldNames(), null, null, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            TaskAttachment attachment = new TaskAttachment(cursor);
            attachments.add(attachment);
        }
    }

    /**
     * @return Parent task.
     */
    public Task getTask() {
        return task;
    }

    /**
     * Add attachment.
     * @param attachment Add this attachment.
     */
    public void add(TaskAttachment attachment) {
        attachments.add(attachment);
        addedAttachments.add(attachment);
    }

    /**
     * Remove attachment.
     * @param attachment Remove this attachment.
     */
    public void remove(TaskAttachment attachment) {
        attachments.remove(attachment);
        deletedAttachments.add(attachment);
    }
}
